package com.han.school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.han.school.pojo.Student;
import com.han.school.pojo.Stuleave;
import com.han.school.service.StuLeaveService;
import com.han.school.service.StudentService;
import com.han.school.vo.StuLeaveVo;
import com.han.school.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import utils.HzdlJsonResult;

import java.util.List;

@RestController
public class StudentController {
    @Autowired StudentService studentService;
    @Autowired
    StuLeaveService stuLeaveService;

    @RequestMapping("/findAllStu")
    public HzdlJsonResult findAllStu(StudentVo studentVo){
        PageInfo<StudentVo> allStu = studentService.findAllStu(studentVo);
        return  HzdlJsonResult.ok(allStu);
    }

    @RequestMapping("/findAllStu1")
    public HzdlJsonResult findAllStu1(StudentVo studentVo){
        PageInfo<StudentVo> allStu = studentService.findAllStu1(studentVo);
        return  HzdlJsonResult.ok(allStu);
    }
    @RequestMapping("/findAllStuById")
    public StudentVo findAllStuById(Integer id){
        StudentVo allStu = studentService.findAllStuById(id);
        return  allStu;
    }

    @RequestMapping("/addStu")
    public String addStu(Student student){
        System.out.println(student);
        Integer row = studentService.addStu(student);
        if (row>0){
            return "添加成功";
        }
        return "添加失败";
    }

    @RequestMapping("/deleteStu")
    public String deleteStu(StudentVo studentVo){
        Integer row = studentService.deleteStu(studentVo);
        if (row>0){
            return "删除成功";
        }
        return "删除失败";
    }
    @RequestMapping("/updateStu")
    public String updateStu(StudentVo studentVo){
        System.out.println(studentVo.getSid());
        Integer row = studentService.updateStu(studentVo);
        if (row>0){
            return "更新成功";
        }
        return "更新失败";
    }

    //请假
    @RequestMapping("askLeave")
    @ResponseBody
    public Integer askLeave(Stuleave stuleave){
        int insert = stuLeaveService.insert(stuleave);
        return insert;
    }
    @RequestMapping("StuLeaveAll")
    @ResponseBody
    public HzdlJsonResult askLeave(StuLeaveVo stuleave){
        PageInfo<Stuleave> stuleavePageInfo = stuLeaveService.selectAll(stuleave);
        return  HzdlJsonResult.ok(stuleavePageInfo.getList(),Long.valueOf(stuleavePageInfo.getTotal()).intValue());
    }

    @RequestMapping("StuAudit")
    @ResponseBody
    public Integer stuAudit(Stuleave stuleave){
        int i = stuLeaveService.updateByStuname(stuleave);
        return  i;
    }
}
